package com.iptv.grape.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.iptv.grape.R;
import com.iptv.grape.bu.AppConfig;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.bu.imp.QuitBackMode;
import com.iptv.grape.bu.imp.QuitTimeLiveMode;
import com.iptv.grape.domain.Channel;
import com.iptv.grape.domain.ChannelBill;
import com.iptv.grape.ui.control.ad.ListViewAdapter;
import com.iptv.grape.ui.control.ad.ListViewUI;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;

// 节目单 预约对话框
public class ChannelBillDlg implements View.OnClickListener {
    private Dialog dialog;
    private View view;
    private TextView tvTitle;
    private TextView tvBillName;
    private TextView tvChannelName;
    private TextView tv_btn;
    private boolean isAppointment = false;
    private Channel channel;
    private ChannelBill channelBill;
    private ListViewUI listViewUI;
    private boolean isAppointmentReminder = false;

    public ChannelBillDlg(Context context, Channel channel, ChannelBill channelBill, boolean isAppointment, ListViewUI listViewUI){
        view = LayoutInflater.from(context).inflate(R.layout.bill_item_dlg,null);
        dialog = new Dialog(context,R.style.AppointmentDialog);
        tvTitle = view.findViewById(R.id.tv_title);
        tvBillName = view.findViewById(R.id.tv_hint);
        tvChannelName = view.findViewById(R.id.tv_time);
        // 按钮
        tv_btn = view.findViewById(R.id.tv_btn);
        tv_btn.setOnClickListener(this);
        tvBillName.setText(channelBill.getText() + "  即将播出");
        tvChannelName.setText(channel.getText() + " " + CommUtil.formatDate(channelBill.getBillStartDate(),"HH:mm"));
        this.isAppointment = isAppointment;
        this.channel = channel;
        this.channelBill = channelBill;
        this.listViewUI = listViewUI;
        if(this.isAppointment ){
            tv_btn.setText("取消预约");
        }else {
            tv_btn.setText("立即预约");
        }
        autoCloseDlg();
    }

    private Handler autoHandle = new Handler();

    private Runnable autoHandleRunnable = new Runnable() {

        @Override
        public void run() {
            dismiss();
        }
    };

    public void closeHandle(){
        autoHandle.removeCallbacks(autoHandleRunnable);
    }

    public void autoCloseDlg(){
        autoHandle.removeCallbacks(autoHandleRunnable);
        autoHandle.postDelayed(autoHandleRunnable, AppConfig.getAppConfig().getAutoCloseMenuTime() * 1000);
    }

    public ChannelBillDlg(Context context, Channel channel, ChannelBill channelBill){
        view = LayoutInflater.from(context).inflate(R.layout.bill_item_dlg,null);
        dialog = new Dialog(context,R.style.AppointmentDialog);
        tvTitle = view.findViewById(R.id.tv_title);
        tvBillName = view.findViewById(R.id.tv_hint);
        tvChannelName = view.findViewById(R.id.tv_time);
        // 按钮
        tv_btn = view.findViewById(R.id.tv_btn);
        tv_btn.setOnClickListener(this);
        tvBillName.setText(channelBill.getText() + "  即将播出");
        tvChannelName.setText(channel.getText() + " " + CommUtil.formatDate(channelBill.getBillStartDate(),"HH:mm"));
        this.isAppointment = false;
        this.channel = channel;
        this.channelBill = channelBill;
        this.listViewUI = null;
        tv_btn.setText("立即观看");
        this.isAppointmentReminder = true;
        autoCloseDlg();
    }


    public void show() {
        dialog.getWindow().setContentView(view);

        dialog.getWindow().setLayout(
                CommUtil.dip2px(GrapeContext.curGrapeContext().getCurActivity(),
                AppConfig.getAppConfig().getChannelBillDlgWidth()),
                CommUtil.dip2px(GrapeContext.curGrapeContext().getCurActivity() ,400));
        dialog.show();
        tv_btn.requestFocus();
        //dialog.getWindow().setLayout(500,LayoutParams.WRAP_CONTENT);
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean isShowing() {
        if (dialog != null) {
            return dialog.isShowing();
        } else {
            return false;
        }
    }


    @Override
    public void onClick(View view) {
        //AppLog.getCurLog().out("按钮OK");
        closeHandle();
        dismiss();
        // 当前的播放界面类型(直播 = Live 点播 = Vod)

        GrapeContext grapeContext = GrapeContext.curGrapeContext();
        String curPlayType = grapeContext.curPlayType();
           if (this.isAppointmentReminder&&"Live".equalsIgnoreCase(curPlayType)) {
               // 当前的直播状态 Live = 直播流 时移 = Time 回看 = Back
               String curLiveType =grapeContext.curLiveType();
               GrapeContext.curGrapeContext().getCurActivity().hideChangingLiveUI();
               grapeContext.setChangingLive(false);
               switch (curLiveType){
                   case "Live":
                       // 立即跳转
                       Channel mChannel = GrapeContext.curGrapeContext().getChannelDLinkList().getCurChannel();
                       if(channel!=null&& !mChannel.getKeyID().equalsIgnoreCase(channel.getKeyID()))
                           this.channel.playChannel();
                       break;
                   case "Time":
                       QuitTimeLiveMode.quit(grapeContext.getCurActivity(),grapeContext.getCurMediaPlayer(),grapeContext,null,channel);
                       break;
                   case "Back":
                       QuitBackMode.execQuitBackMode(GrapeContext.curGrapeContext().getCurActivity(),channel);
                       break;
               }

           } else {
               if (!this.isAppointment) {
                   // 添加到预约列表
                   GrapeContext.curGrapeContext().getBillAppointmentContainer().add(this.channel, this.channelBill);
               } else {
                   // 从预约列表中删除对象
                   GrapeContext.curGrapeContext().getBillAppointmentContainer().setState(this.channel, this.channelBill);
               }
               ListViewAdapter lvAdapter = (ListViewAdapter) listViewUI.getListViewVScroll().getAdapter();
               lvAdapter.notifyDataSetInvalidated();
               lvAdapter.notifyDataSetChanged();
           }


    }
}
